package com.maket.scxx.controller;

import com.maket.scxx.domain.*;
import com.maket.scxx.feign.GetCodeFeign;
import com.maket.scxx.service.GylxConfigService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.core.utils.PageUtils.startPage;
import static com.ruoyi.common.core.web.domain.AjaxResult.success;

@SuppressWarnings("all")
@RestController
@RequestMapping("/gyRoadConfig")
public class GylxConfigController extends BaseController {
    @Autowired
    private GylxConfigService gylxConfigService;

    /**
     * 查询工艺路线配置详情
     */
    @RequiresPermissions("scxx:gyRoad:list")
    @GetMapping("/gyRoadMore")
    public AjaxResult list(GxProcessAndGylxProcessRoute gylxProcessRoute)
    {
        GylxProcessRoute gyroadCode = gylxConfigService.selectWithGylxAndGx(gylxProcessRoute);
        return success(gyroadCode);
    }

    /**
     * 查询工序定义列表
     */
    @RequiresPermissions("scxx:gyRoad:list")
    @GetMapping("/listGx")
    public TableDataInfo list(GxProcess gxProcess)
    {
        startPage();
        List<GxProcess> list = gylxConfigService.selectGxProcessList(gxProcess);
        for (GxProcess process : list) {
            process.setGyroadCode(gxProcess.getGyroadCode());
        }
        return getDataTable(list);
    }

    /**
     * 新增工艺路线工序
     */
    @RequiresPermissions("scxx:gyRoad:add")
    @Log(title = "工艺路线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<GxProcessAndGylxProcessRoute> multipleSelectionGx)
    {
        int i = 0;
        for (GxProcessAndGylxProcessRoute selectionGx : multipleSelectionGx) {
            gylxConfigService.insertGylxAndGx(selectionGx);
            i++;
        }
        return toAjax(i);
    }

    /**
     * 删除工艺路线工序
     */
    @RequiresPermissions("scxx:gyRoad:remove")
    @Log(title = "工艺路线", businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(@RequestBody GxProcessAndGylxProcessRoute gxProcessAndGylxProcessRoute)
    {
        return toAjax(gylxConfigService.deleteGxAndGylx(gxProcessAndGylxProcessRoute));
    }

    /**
     * 查询生产单元列表
     */
    @RequiresPermissions("scxx:gyRoad:list")
    @GetMapping("/listScdy")
    public TableDataInfo list(GcFactoryUnitTable gcFactoryUnitTable)
    {
        startPage();
        List<GcFactoryUnitTable> list = gylxConfigService.getAllScdy(gcFactoryUnitTable);
        for (GcFactoryUnitTable gc : list) {
            gc.setGyroadCode(gcFactoryUnitTable.getGyroadCode());
            gc.setGxCode(gcFactoryUnitTable.getGxCode());
        }
        return getDataTable(list);
    }

    /**
     * 新增工艺路线工序生产单元
     */
    @RequiresPermissions("scxx:gyRoad:add")
    @Log(title = "工艺路线", businessType = BusinessType.INSERT)
    @PostMapping("/addScdy")
    public AjaxResult addScdy(@RequestBody List<GxProcessAndScwzRoductionLocation> gcFactoryUnitTables)
    {
        int i = 0;
        for (GxProcessAndScwzRoductionLocation gcFactoryUnitTable : gcFactoryUnitTables) {
            gylxConfigService.insertGxAndScdy(gcFactoryUnitTable);
            i++;
        }
        return toAjax(i);
    }

    /**
     * 删除工艺路线工序生产单元
     */
    @RequiresPermissions("scxx:gyRoad:remove")
    @Log(title = "工艺路线", businessType = BusinessType.DELETE)
    @DeleteMapping("/delScdy")
    public AjaxResult remove(@RequestBody GxProcessAndScwzRoductionLocation gcFactoryUnitTable)
    {
        return toAjax(gylxConfigService.deleteGxAndScdy(gcFactoryUnitTable));
    }

    /**
     * 查询生产单元列表
     */
    @RequiresPermissions("scxx:gyRoad:list")
    @GetMapping("/listUser")
    public TableDataInfo list(GxUser sysUser)
    {
        startPage();
        List<GxUser> list = gylxConfigService.getAllUser(sysUser);
        for (GxUser gc : list) {
            gc.setGyroadCode(sysUser.getGyroadCode());
            gc.setGxCode(sysUser.getGxCode());
        }
        return getDataTable(list);
    }

    /**
     * 新增工艺路线工序生产单元
     */
    @RequiresPermissions("scxx:gyRoad:add")
    @Log(title = "工艺路线", businessType = BusinessType.INSERT)
    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestBody List<GxProcessAndSysUser> gxProcessAndSysUsers)
    {
        int i = 0;
        for (GxProcessAndSysUser gcFactoryUnitTable : gxProcessAndSysUsers) {
            gylxConfigService.insertGxAndUser(gcFactoryUnitTable);
            i++;
        }
        return toAjax(i);
    }

    /**
     * 删除工艺路线工序生产单元
     */
    @RequiresPermissions("scxx:gyRoad:remove")
    @Log(title = "工艺路线", businessType = BusinessType.DELETE)
    @DeleteMapping("/delUser")
    public AjaxResult remove(@RequestBody GxProcessAndSysUser gcFactoryUnitTable)
    {
        return toAjax(gylxConfigService.deleteGxAndUser(gcFactoryUnitTable));
    }

    /**
     * 修改工艺路线工序顺序
     */
    @RequiresPermissions("scxx:gyRoad:edit")
    @Log(title = "工艺路线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<GxProcessAndGylxProcessRoute> gxProcessAndGylxProcessRoute)
    {
        int i = 0;
        for (GxProcessAndGylxProcessRoute processAndGylxProcessRoute : gxProcessAndGylxProcessRoute) {
            gylxConfigService.updateSort(processAndGylxProcessRoute);
            i++;
        }
        return toAjax(i);
    }

    /**
     * 查询工序
     */
    @RequiresPermissions("scxx:gyRoad:list")
    @Log(title = "工艺路线", businessType = BusinessType.UPDATE)
    @GetMapping("/{gxCode}")
    public TableDataInfo select(@PathVariable String gxCode)
    {
        return getDataTable(gylxConfigService.selectWithGxCode(gxCode));
    }
}
